export default class Layer {
	constructor(viewer) {
		this.viewer = viewer;
		this._init();
	}
	_init() {
		this.model3D = this.viewer.getModel();
		this.model3D.getModelTree((data) => {
			console.log(data, 'data');
			// this.layerGroups = data[0].items;
			if (Array.isArray(data)) {
				data.forEach((item) => {
					if (item.items) {
						delete item.items;
					}
				});
				this.layerGroups = data;
			} else {
				this.layerGroups = [];
			}
		});
	}
	// id  外部传入id 根据id映射到name 控制显隐
	get(id) {
		if (!id) {
			// return this.layerGroups;
			if (!this.layerGroups.length) {
				return Promise.resolve('当前模型无图层');
			} else {
				return Promise.resolve(this.layerGroups);
			}
		}
		const arr = this.layerGroups.filter((item) => {
			return item.id == id;
		});
		// return arr;
		return Promise.resolve(arr);
	}
	show = (ids) => {
		if (!ids) return;
		if (Array.isArray(ids)) {
			const nameArr = [];
			this.layerGroups.forEach((item) => {
				if (ids.indexOf(item.id) !== -1) {
					nameArr.push(item.name);
				}
			});
			this.model3D.showComponentsByObjectData([{ levelName: nameArr }]);
		} else {
			const { name } = this.layerGroups.find((item) => item.id === ids);
			this.model3D.showComponentsByObjectData([{ levelName: name }]);
		}

		this.viewer.render();
	};
	hide = (ids) => {
		if (!ids) return;
		if (Array.isArray(ids)) {
			const nameArr = [];
			this.layerGroups.forEach((item) => {
				if (ids.indexOf(item.id) !== -1) {
					nameArr.push(item.name);
				}
			});
			this.model3D.hideComponentsByObjectData([{ levelName: nameArr }]);
		} else {
			const { name } = this.layerGroups.find((item) => item.id === ids);
			this.model3D.hideComponentsByObjectData([{ levelName: name }]);
		}
		this.viewer.render();
	};
}
